package weaver.interfaces.haozhi;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * 晧智考勤数据同步到表单建模表中，每四小时同步一次，每次同步当天所有数据
 * Created by Administrator on 2017/7/31.
 */
public class SynchronousAttendanceData2 extends BaseCronJob {
    private static BaseBean bs = new BaseBean();
    @Override
    public void execute() {
        writeLog("=======进入SynchronousAttendanceData=======");
        String sql = "";
        //考勤机数据集对象
        RecordSetDataSource kqrs = new RecordSetDataSource("KQJ");
        RecordSet oars = new RecordSet();
        writeLog("=======查询考勤机数据（当天的）=======");
//        sql = "SELECT u.badgenumber,u.name,ck.* FROM userinfo u,checkinout ck WHERE u.badgenumber = ck.pin and ck.checktime > (select CONVERT(varchar(100), GETDATE(), 23))ORDER BY checktime DESC";
        sql = "SELECT u.badgenumber,u.name,ck.* FROM userinfo u,checkinout ck WHERE u.badgenumber = ck.pin";
        kqrs.execute(sql);
        //开始同步
        writeLog("=======开始同步考勤机数据=======");
        String userid = "";
        String name = "";
        String checktime = "";
        String checktype = "";
        String snname = "";
        int count  = 1;
        while (kqrs.next()){
            writeLog("=======进入循环=======");
            userid = String.valueOf(Util.getIntValue(kqrs.getString("badgenumber")));
            name = Util.null2String(kqrs.getString("name"));
            checktime = Util.null2String(kqrs.getString("checktime"));
            checktype = Util.null2String(kqrs.getString("checktype"));
            snname = Util.null2String(kqrs.getString("sn_name"));
            //处理数据
            //获取日期 和  时间
            String []datatime = checktime.split(" ");
            String date = datatime[0];
            String time = datatime[1].substring(0,datatime[1].length()-5);
            //查询人员id
//            sql = "select id from hrmresource where loginid = '"+name+"'";
//            oars.execute(sql);
//            if (oars.next()){
//                name = oars.getString("id");
//            }

            //同步到OA 表单建模
            sql = "select * from uf_kqxx where userid = '"+userid+"' and checkdate = '"+date+"' and checktime = '"+time+"' ";
            oars.execute(sql);
            //不存在继续同步，存在跳过
            if (!oars.next()){
                writeLog("=======数据不存在，开始同步【"+name+"】=======");
                sql = "insert into uf_kqxx(userid,name,checkdate,checktime,checktype,snname,formmodeid,modedatacreater) values('"+userid+"','"+name+"','"+date+"','"+time+"','"+checktype+"','"+snname+"','4','1');";
                boolean b = oars.executeUpdate(sql);
                writeLog(""+b);
            }
            count++;
        }
        //全部权限权限重构
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.init();
        modeRightInfo.setModeId(4);
        modeRightInfo.resetModeRight();
        writeLog("=======所有数据同步完成，共："+count+"条数据=======");
    }

    //打印日志
    private static void writeLog(String s){
        bs.writeLog(s);
    }

    public static void main(String[] args) {
        String str = "2017-07-31 13:27:12.0";
        //获取日期 和  时间
        String []datatime = str.split(" ");
        String data = datatime[0];
        String time = datatime[1].substring(0,datatime[1].length()-5);
        System.out.println(data+"++===++"+time);
    }
}
